//
//  RHDeviceDataController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/27.
//

#import "RHDeviceDataController.h"
#import "RHDeviceItemDataCell.h"
#import "RHDeviceChartDataCell.h"
#import "RHDeviceTotalDataCell.h"
#import "RHFirmwareListVC.h"
#import "RHDeviceDataModel.h"
#import "RHDeviceModel.h"
#import "DevChartModel.h"
#import "DevChartTypeModel.h"
#import "DeviceChartFieldModel.h"
#import "DevChartParamsModel.h"
#import "ChoiceEndpointVC.h"
#import "RHWindowTipManage.h"
#import "DevParamsAlertController.h"
#import "WSDatePickerView.h"
#import "RHChartHeaderView.h"
#import "DeviceFullChartController.h"
#import "RHEndPointTypeModel.h"
#import "RHEndPointModel.h"
#import "RHTopoModel.h"
#import "RHDeviceDataSectionView.h"
#import "RHFieldsDataVC.h"

@interface RHDeviceDataController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) RHChartHeaderView *headerView;

@property (nonatomic ,assign) __block NSInteger modelType;

//曲线参数,通过端点类型创建
@property (nonatomic ,strong) DevChartParamsModel *chartParamsModel;

@property (nonatomic ,strong) ChoiceEndpointVC *choiceEndpointVC;


//当前选中的子端点
@property(nonatomic,strong)RHEndPointModel *curEndPointModel;

@property(nonatomic,copy)NSString *ems_sn;
//各型号的当前版本
@property(nonatomic,copy)NSString *curTypeVersion;


@end

@implementation RHDeviceDataController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:CFTabViewBgColor];

    //根据端点类型模型创建曲线模型
    self.chartParamsModel = [DevChartTypeModel creatChartParams:ChartType_Master];
    self.chartParamsModel.chartList = @[];
    self.chartParamsModel.formFields = @[];
    [self getParamList];
    [self setupUI];
   
}

#pragma mark --------------- 1. 设置大端点类型 默认选择第一个个子端点进行获取数据点和曲线要显示的参数
-(void)setEndPointTypeModel:(RHEndPointTypeModel *)endPointTypeModel
{
    _endPointTypeModel = endPointTypeModel;
    if(endPointTypeModel.nodes.count>0){
        self.curEndPointModel = [endPointTypeModel.nodes firstObject];
    }
    
}



#pragma mark --------------- 2. 选择子端点 获取要展示的参数-------
//选择大的类型
- (void)getParamList
{
   
    kWeakSelf;
    //获取要显示的曲线和底部数据 参数
   
    NSDictionary *para = @{
        @"endpointTypeId": @(self.endPointTypeModel.endpointTypeId),
        @"styles": @[@(1), @(2)],
        @"deviceTypeCode":self.curDevModel.deviceTypeCode
       };
    [DeviceChartFieldModel startWithCompletionAPIKey:H_getDataByStyle argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        NSArray *dataArr = resultModel.data;
        NSMutableArray *chartDataArr = [NSMutableArray array];
        NSMutableArray *fieldDataArr = [NSMutableArray array];
        
        [dataArr enumerateObjectsUsingBlock:^(DeviceChartFieldModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if(obj.displayStyle == 1){
                [chartDataArr addObject:obj];
            }
            if(obj.displayStyle == 2){
                [fieldDataArr addObject:obj];
            }
        }];
        //设置底部参数
        self.chartParamsModel.formFields = fieldDataArr.copy;
        
        NSMutableArray *unitArr = [NSMutableArray array];
        //提取单位,并去重 对参数进行分组,相同单位的分成一组,每组最多两个单位
        [chartDataArr enumerateObjectsUsingBlock:^(DeviceChartFieldModel  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if(![unitArr containsObject:obj.unitName]){
                [unitArr addObject:obj.unitName];
            }
        }];
        //算出一共有多少组,初始化装单位的数组
        NSInteger unitArrCount = unitArr.count%2 ? unitArr.count/2+1 : unitArr.count/2;
        NSMutableArray *unit_slicArr = [NSMutableArray array];
        for (int i = 0; i < unitArrCount; i++) {
            NSMutableArray *temp = [NSMutableArray array];
            [unit_slicArr addObject:temp];
        }
        //拆分成两个单位一组
        [unitArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger index = idx/2;
            NSMutableArray *arr = unit_slicArr[index];
            [arr addObject:obj];
        }];
        
        //将数据 按两个单位一组
        NSMutableArray *chartListArr = [NSMutableArray array];
        
        [unit_slicArr enumerateObjectsUsingBlock:^(NSMutableArray  *_Nonnull unitArray, NSUInteger idx, BOOL * _Nonnull stop) {
            //每条曲线的数据点
            NSMutableArray *chartModelArr = [NSMutableArray array];
            [chartDataArr enumerateObjectsUsingBlock:^(DeviceChartFieldModel  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if([unitArray containsObject:obj.unitName]){
                    [chartModelArr addObject:obj];
                }
            }];

            //创建曲线模型
            DevChartModel *chartModel = [[DevChartModel alloc]init];
            chartModel.selectArray = chartModelArr;
            chartModel.dayParams = chartModelArr.copy;
            chartModel.monthParams = chartModelArr.copy;
            chartModel.yearParams = chartModelArr.copy;
            chartModel.params = chartModelArr.copy;
            chartModel.timeType = TimeType_Day;
            NSDate *date = [NSDate date];
            NSTimeInterval interval = [date timeIntervalSince1970];
            chartModel.curTime = [NSString stringWithFormat:@"%d",(int)interval];
            
            [chartListArr addObject:chartModel];
        }];
        

        [self.chartParamsModel setNetworkChartList: chartListArr.copy];
        
        //设置曲线参数
//        [self.chartParamsModel.chartList enumerateObjectsUsingBlock:^(DevChartModel  *_Nonnull chartModel, NSUInteger idx, BOOL * _Nonnull stop) {
//            
//            if (weakSelf.curDevModel) {
//                if (chartModel.timeType == TimeType_Day) {
//                    //获取曲线数据
//                    [weakSelf getDataFromNetwork:chartModel];
//                }else if (chartModel.timeType == TimeType_Month){
//                   
//                }else if (chartModel.timeType == TimeType_Year){
//                   
//                }
//            }else{
//                [weakSelf reloadTableCell];
//            }
//        }];
        
        [self.tableView reloadData];
        
        [self getDeviceData];
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}

#pragma mark --------------- 3. 获取 数据点 数据 ----

- (void)getFormData{
    
    if(!self.curEndPointModel){
        return;
    }
    NSMutableArray *paraKey = [NSMutableArray array];
    for (DeviceChartFieldModel *fileld in self.chartParamsModel.formFields) {
        [paraKey addObject:fileld.keyCode];
    }
    NSDictionary *para = @{@"deviceId":self.curDevModel.deviceId,
                           @"endpointTypeCode":self.curEndPointModel.endpointCode,
                           @"keyCodes":paraKey,
                           @"endpointSn":self.curEndPointModel.sn
    };
    [BaseModel startWithCompletionAPIKey:D_getLastEndpointData argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        if(resultModel.code == 200){
            if([request.responseObject[@"data"] isKindOfClass:[NSDictionary class]]){
                self.chartParamsModel.formFieldDataArray = @[request.responseObject[@"data"]];
                [self.tableView reloadData];
            }
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
    }];
}


#pragma mark --------------- 4.获取曲线数据 -----

- (void)getDataFromNetwork:(DevChartModel *)chartModel{
    
    if (!_curDevModel) {
        return;
    }
    
    NSMutableArray *keys = [NSMutableArray array];
    for (DeviceChartFieldModel *fileld in chartModel.selectArray) {
        [keys addObject:fileld.keyCode];
    }

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[chartModel.curTime integerValue]];
    NSDate *begin_date = [NSDate dateWithYear:date.year month:date.month day:date.day hour:0 minute:0 second:0];
    NSDate *end_date = [NSDate dateWithYear:date.year month:date.month day:date.day hour:23 minute:59 second:59];
    
    NSString *begin_time = [NSString stringWithFormat:@"%zd",(NSInteger)[begin_date timeIntervalSince1970]*1000];
    NSString *end_time = [NSString stringWithFormat:@"%zd",(NSInteger)[end_date timeIntervalSince1970]*1000];
    
    
    if(!self.curEndPointModel){
        NSLog(@"------->请选择子端点");
        [self.tableView endHeaderRefresh];
        return;
    }
    
    NSDictionary *para = @{@"deviceId":self.curDevModel.deviceId,
                           @"endpointTypeCode":self.endPointTypeModel.endpointTypeCode,
                           @"keyCodes":keys,
                           @"endpointSn":self.curEndPointModel.sn,
                           @"startTimestamp":begin_time,
                           @"endTimestamp":end_time
    };
    [QMUITips showLoadingInView:self.view];
    [BaseModel startWithCompletionAPIKey:D_getEndpointDataByTime argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        if(resultModel.code == 200){
            if([request.responseObject[@"data"] isKindOfClass:[NSArray class]]){
                NSArray *dataArr = request.responseObject[@"data"];
                
                chartModel.allDataArray = dataArr;

                [self reloadTableCell];
            }
        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        
    }];
}

#pragma mark --------------- 更新端点下的子端点在线状态
-(void)updateEndpointTypeModel:(RHEndPointTypeModel*)typeModel
{
    if (self.endPointTypeModel.endpointTypeId == typeModel.endpointTypeId) {
        [self.endPointTypeModel.nodes enumerateObjectsUsingBlock:^(RHEndPointModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
            [typeModel.nodes enumerateObjectsUsingBlock:^(RHEndPointModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([model.sn isEqualToString:obj.sn]) {
                    model.state = obj.state;
                }
            }];
        }];
    }
    self.headerView.endPointModel = self.headerView.endPointModel;
}


#pragma mark -----  Action
- (void)fullAction:(NSInteger)index{
    DevChartModel *chartModel = self.chartParamsModel.chartList[index];
    NSInteger type = 2;
//    if (chartModel.monthParams.count>0) {
//        type = 1;
//    }
//    if (chartModel.yearParams.count>0) {
//        type = 0;
//    }
    
    DevChartModel *fullModel = [chartModel modelCopy];
    DeviceFullChartController *vc = [[DeviceFullChartController alloc] initWithModel:fullModel devModel:self.curDevModel chartType:self.chartParamsModel.type toolType:type endPointModel:self.curEndPointModel];
    [self.navigationController pushViewController:vc animated:NO];
}

#pragma mark ----- 参数 , 年 , 月 , 日选项 -------------
//工具选择项
- (void)selectTopTool:(NSInteger)index atIndexpath:(NSIndexPath *)indexpath{
    DevChartModel *chartModel = self.chartParamsModel.chartList[indexpath.row];
    
    if (index == -1) {//Params
        DevParamsAlertController *vc = [[DevParamsAlertController alloc] initWithModel:chartModel isFull:NO maxSelectCount:chartModel.maxSelectCount];
        [[RHWindowTipManage shareWindowTipManage] showWithController:vc];
        kWeakSelf;
        vc.resultBlock = ^{
            [weakSelf updateParams:chartModel];
        };
        return;
    }

    if (index == 0) {//Day
        chartModel.timeType = TimeType_Day;
        NSDate *date = [NSDate date];
        NSTimeInterval interval = [date timeIntervalSince1970];
        chartModel.curTime = [NSString stringWithFormat:@"%d",(int)interval];
        chartModel.params = chartModel.dayParams;
        [self getDataFromNetwork:chartModel];
        return;
    }
    
    if (index == 1) {//Month
        chartModel.timeType = TimeType_Month;
        NSDate *date = [NSDate date];
        NSTimeInterval interval = [date timeIntervalSince1970];
        chartModel.curTime = [NSString stringWithFormat:@"%d",(int)interval];
        chartModel.params = chartModel.monthParams;
        chartModel.allDataArray = @[];
        [self reloadTableCell];
//        [self getYearOrMonth:YES model:chartModel];
        return;
    }
    
    if (index == 2) {//Year
        chartModel.timeType = TimeType_Year;
        NSDate *date = [NSDate date];
        NSTimeInterval interval = [date timeIntervalSince1970];
        chartModel.curTime = [NSString stringWithFormat:@"%d",(int)interval];
        chartModel.params = chartModel.yearParams;
        chartModel.allDataArray = @[];;
        [self reloadTableCell];
//        [self getYearOrMonth:NO model:chartModel];
        return;
    }
    
    if (index == 4) {//Total
        chartModel.timeType = TimeType_Total;
        return;
    }

    
}
#pragma mark ------ 选择日期 ------
//变更日期
- (void)changeDate:(NSIndexPath *)indexpath{
    DevChartModel *chartModel = self.chartParamsModel.chartList[indexpath.row];
    //NSString *dateFormatStr = @"yyyy-MM-dd HH:mm";
    WSDateStyle dataStyle = DateStyleShowYearMonthDay;
    if (chartModel.timeType == TimeType_Day) {
        dataStyle = DateStyleShowYearMonthDay;
    }else if (chartModel.timeType == TimeType_Month){
        dataStyle = DateStyleShowYearMonth;
    }else if (chartModel.timeType == TimeType_Year){
        dataStyle = DateStyleShowYearMonthDay;
    }

    @weakify(self);
    WSDatePickerView *datePickerV = [[WSDatePickerView alloc] initWithDateStyle:dataStyle CompleteBlock:^(NSDate *selectDate) {
        @strongify(self);
        //NSString *currentDate = [selectDate stringWithFormat: dateFormatStr];//年-月-日-时-分
        NSUInteger timeSp = [selectDate timeIntervalSince1970];
        chartModel.curTime = [NSString stringWithFormat:@"%d",(int)timeSp];
        if (chartModel.timeType == TimeType_Day) {
            [self getDataFromNetwork:chartModel];
        }else if (chartModel.timeType == TimeType_Month){
//            [self getYearOrMonth:YES model:chartModel];
            [self reloadTableCell];
        }else if (chartModel.timeType == TimeType_Year){
//            [self getYearOrMonth:NO model:chartModel];
            [self reloadTableCell];
        }
    }];
    datePickerV.dateLabelColor = FontColor(@"#3ECE80");//年-月 颜色
    datePickerV.datePickerColor = UIColorBlack;//滚轮日期颜色
    datePickerV.doneButtonColor = UIColor.blackColor;//确定按钮的颜色
    if (chartModel.timeType == TimeType_Day) {
        datePickerV.maxLimitDate = [NSDate date];
        datePickerV.minLimitDate = [NSDate dateWithYear:[NSDate date].year-5 month:[NSDate date].month day:[NSDate date].day hour:[NSDate date].hour minute:[NSDate date].minute second:[NSDate date].second];
    }else if (chartModel.timeType == TimeType_Month){

    }else if (chartModel.timeType == TimeType_Year){
        datePickerV.maxLimitDate = [NSDate date];
        datePickerV.minLimitDate = [NSDate dateWithYear:[NSDate date].year-5 month:[NSDate date].month day:[NSDate date].day hour:[NSDate date].hour minute:[NSDate date].minute second:[NSDate date].second];
    }
    [datePickerV show];
}
#pragma mark ------ 前一天 -------
//日期累加
- (void)addTimeAction:(NSIndexPath *)indexpath{
    DevChartModel *chartModel = self.chartParamsModel.chartList[indexpath.row];
    if (chartModel.timeType == TimeType_Total) {
        return;
    }
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[chartModel.curTime integerValue]];
    NSDate *nextDate;
    if (chartModel.timeType == TimeType_Day) {
        nextDate = [date dateByAddingDays:1];
    }else if (chartModel.timeType == TimeType_Month){
        nextDate = [date dateByAddingMonths:1];
    }else if (chartModel.timeType == TimeType_Year){
        nextDate = [date dateByAddingYears:1];
    }
    NSTimeInterval interval = [nextDate timeIntervalSince1970];
    chartModel.curTime = [NSString stringWithFormat:@"%d",(int)interval];
    
    if (chartModel.timeType == TimeType_Day) {
        [self getDataFromNetwork:chartModel];
    }else if (chartModel.timeType == TimeType_Month){
       
        [self reloadTableCell];
    }else if (chartModel.timeType == TimeType_Year){
       
    }
}

#pragma mark ------ 后一天 -------
//日期累减
- (void)subTimeAction:(NSIndexPath *)indexpath{
    DevChartModel *chartModel = self.chartParamsModel.chartList[indexpath.row];
    if (chartModel.timeType == TimeType_Total) {
        return;
    }
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[chartModel.curTime integerValue]];
    NSDate *nextDate;
    if (chartModel.timeType == TimeType_Day) {
        nextDate = [date dateBySubtractingDays:1];
    }else if (chartModel.timeType == TimeType_Month){
        nextDate = [date dateBySubtractingMonths:1];
    }else if (chartModel.timeType == TimeType_Year){
        nextDate = [date dateBySubtractingYears:1];
    }
    NSTimeInterval interval = [nextDate timeIntervalSince1970];
    chartModel.curTime = [NSString stringWithFormat:@"%d",(int)interval];
    
    if (chartModel.timeType == TimeType_Day) {
        [self getDataFromNetwork:chartModel];
    }else if (chartModel.timeType == TimeType_Month){
        
    }else if (chartModel.timeType == TimeType_Year){
        
    }
}




-(void)getDeviceData
{
    //设置曲线参数
    [self.chartParamsModel.chartList enumerateObjectsUsingBlock:^(DevChartModel  *_Nonnull chartModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (self.curDevModel) {
            if (chartModel.timeType == TimeType_Day) {
                [self getDataFromNetwork:chartModel];
            }else if (chartModel.timeType == TimeType_Month){
              
            }else if (chartModel.timeType == TimeType_Year){
                
            }
        }else{
            [self reloadTableCell];
        }
    }];
    
    if (self.curDevModel) {
        //获取底部数据
        [self getFormData];

    }

}







#pragma mark - Action Mothed
- (void)updateParams:(DevChartModel *)chartModel{
    
    if (chartModel.timeType == TimeType_Day) {
        [self getDataFromNetwork:chartModel];
    }else if (chartModel.timeType == TimeType_Month){
      
    }else if (chartModel.timeType == TimeType_Year){
       
    }
}

//手动更新Cell
- (void)reloadTableCell{
    /*[self.chartParamsModel.chartList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DeviceCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
        [cell setChartModel:obj];
    }];*/
    [self.tableView reloadData];
}



- (void)firmwareUpgradeAction{
    RHFirmwareListVC *vc = [[RHFirmwareListVC alloc] init];
    vc.deviceModel = self.curDevModel;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ----- 更多数据点
-(void)seeFieldData
{
    RHFieldsDataVC *vc = [[RHFieldsDataVC alloc]init];
    vc.formFields = self.chartParamsModel.formFields;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setupUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView setEstimatedSectionFooterHeight:0];
    [_tableView setSectionFooterHeight:0];
    [_tableView registerClass:[RHDeviceTotalDataCell class] forCellReuseIdentifier:@"RHDeviceTotalDataCell"];
    [_tableView registerClass:[RHDeviceItemDataCell class] forCellReuseIdentifier:@"RHDeviceItemDataCell"];
    _tableView.estimatedRowHeight = 380;
    _tableView.tableHeaderView = self.headerView;
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    kWeakSelf;
    [_tableView addHeaderRefresh:^{
        [weakSelf getDeviceData];
        if (weakSelf.updateDevStatus) {
            weakSelf.updateDevStatus();
        }
    }];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }

    _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
}

- (UIView *)headerView{
    if(!_headerView){
        _headerView = [[RHChartHeaderView alloc]init];
        _headerView.frame = CGRectMake(0, 0, kUI_WINDOW_W, kScaleW(45)*2+10);
        _headerView.endpointName = self.endPointTypeModel.endpointTypeName;
        if(self.endPointTypeModel.nodes.count>0){
            _headerView.endPointModel = [self.endPointTypeModel.nodes firstObject];
        }
        kWeakSelf;
        _headerView.selectBlock = ^{
            [UIView animateWithDuration:0.2 animations:^{
                weakSelf.headerView.arrowImg.transform = CGAffineTransformMakeRotation(M_PI/2);
            }];
            
            weakSelf.choiceEndpointVC = [[ChoiceEndpointVC alloc]init];
            weakSelf.choiceEndpointVC.endpointName = weakSelf.endPointTypeModel.endpointTypeName;
            weakSelf.choiceEndpointVC.dataList = weakSelf.endPointTypeModel.nodes;
            weakSelf.headerView.endpointName = weakSelf.endPointTypeModel.endpointTypeName;
            weakSelf.choiceEndpointVC.curModel = weakSelf.curEndPointModel;
            [[RHWindowTipManage shareWindowTipManage] showWithController:weakSelf.choiceEndpointVC];
            [weakSelf.choiceEndpointVC showAnimation];
            weakSelf.choiceEndpointVC.choiceResultBlock = ^(RHEndPointModel * _Nonnull model) {
                weakSelf.curEndPointModel = model;
                weakSelf.headerView.endPointModel = model;
                //重新获取子端点下的参数列表
                [weakSelf getParamList];
                [UIView animateWithDuration:0.2 animations:^{
                    weakSelf.headerView.arrowImg.transform = CGAffineTransformIdentity;
                }];
            };
           
        };
    }
    return _headerView;
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.chartParamsModel.chartList.count == 0 && self.chartParamsModel.formFields.count == 0) {
        return 0;
    }
   return self.chartParamsModel ? 2:0;;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (!self.chartParamsModel||section == 0) {
        //最后一个数据
        return self.chartParamsModel.formFields?1:0;
    }
    
    if (section == 1&&self.chartParamsModel.chartList) {
//        return 1;
        return self.chartParamsModel.chartList.count;
    }
    
    
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 && self.chartParamsModel.formFields.count == 0){
        return 0;
    }
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    RHDeviceDataSectionView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    if(header == nil){
        header = [[RHDeviceDataSectionView alloc]initWithReuseIdentifier:@"header"];
    }
    kWeakSelf;
    header.seeMoreBlock = ^{
        [weakSelf seeFieldData];
    };
    if(section == 0){
        header.titleLab.text = @"参数".rh_localized;
        [header hideArrowView:NO];
    }else{
        header.titleLab.text = @"图表数据".rh_localized;
        [header hideArrowView:YES];
    }
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        if (self.chartParamsModel.formFields.count == 0) {
            return 120;
        }
    }
    if(section == 1){
        if (self.chartParamsModel.chartList.count == 0) {
            return 120;
        }
        return 15;
    }
    
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSLog(@"---->返回组尾:%ld",section);
    if(section == 0){
        //无数据点
        if (self.chartParamsModel.formFields.count == 0) {
            //
            UIView *nodataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 120)];
            LYEmptyView *emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
            [nodataView addSubview:emptyView];
            return nodataView;
        }
        
        UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer_0"];
        if(footer == nil){
            footer = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"footer_0"];
            footer.backgroundColor = CFTabViewBgColor;
        }
        return footer;
    }else if(section == 1){
        //无曲线数据
        if (self.chartParamsModel.chartList.count == 0) {
            UIView *nodataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 120)];
            LYEmptyView *emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
            [nodataView addSubview:emptyView];
            return nodataView;
        }
        
        UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer_1"];
        if(footer == nil){
            footer = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"footer_1"];
            footer.frame = CGRectMake(0, 0, kUI_WINDOW_W, 10);
            UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 10)];
            footView.backgroundColor = [UIColor clearColor];
            UIView *bgView = [[UIView alloc]init];
            [footView addSubview:bgView];
            bgView.backgroundColor = [UIColor whiteColor];
            bgView.frame = CGRectMake(15, 0, kUI_WINDOW_W-30, 10);
            bgView.layer.cornerRadius = 5;
            bgView.layer.maskedCorners = kCALayerMinXMaxYCorner|kCALayerMaxXMaxYCorner;
            [footer addSubview:footView];
        }
       
        return footer;
    }
    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"----->返回:%ld组 %ld行",indexPath.section,indexPath.row);
    //数据点
    if(indexPath.section == 0){
        RHDeviceItemDataCell *itemCell = [tableView dequeueReusableCellWithIdentifier:@"RHDeviceItemDataCell"];
        if (self.chartParamsModel.formFields) {
            [itemCell setFormFields:self.chartParamsModel.formFields deviceTypeCode:self.curDevModel.deviceTypeCode];
        }
        [itemCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return itemCell;
    }else if(indexPath.section == 1){
        //曲线
        RHDeviceChartDataCell *cell;
        DevChartModel *chartModel = self.chartParamsModel.chartList[indexPath.row];

        NSString *type = @"Day";
//        if (chartModel.monthParams.count>0) {
//            type = @"Month";
//        }
//        if (chartModel.yearParams.count>0) {
//            type = @"Year";
//        }
        NSString *reuseIdentifier = [NSString stringWithFormat:@"DeviceCell_%ld_%ld_%@",(long)indexPath.section,(long)indexPath.row,type];
        NSLog(@"----->identify:%@",reuseIdentifier);
        cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            cell = [[RHDeviceChartDataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setChartModel:chartModel];
        kWeakSelf;
        //全屏
        cell.fullChartBlock = ^{
            [weakSelf fullAction:indexPath.row];
        };
        //选择参数
        cell.selectTopToolBlock = ^(NSInteger index) {
            [weakSelf selectTopTool:index atIndexpath:indexPath];
        };
        
        cell.subTimeBlock = ^{//日期往前
            [weakSelf subTimeAction:indexPath];
        };
        
        cell.addTimeBlock = ^{//日期往后
            [weakSelf addTimeAction:indexPath];
        };
        
        cell.changeTimeBlock = ^{//选择日期
            [weakSelf changeDate:indexPath];
        };
        
        return cell;
    }else if (indexPath.section == 2) {
        
    }
    
    return [UITableViewCell new];
}




#pragma mark - JXCategoryListContentViewDelegate
/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}




@end
